package com.groupbuying.customerconsumer.utils;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.groupbuying.customerconsumer.constants.CustomerAlipayConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class AliUntil {
    private static final Logger logger = LoggerFactory.getLogger(AliUntil.class);

    /**
     *获取用户信息
     * @param accessToken
     *
     * @return
     */

    public static  AlipayUserInfoShareResponse getUserInfo(String accessToken,Map<String,String> param) throws Exception{


        AlipayClient alipayClient = new DefaultAlipayClient(param.get("url"),param.get("appId"),
                param.get("appPrivateKey"),param.get("format"),param.get("charset"),
                param.get("alipayPublicKey"),param.get("signType"));

        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken);
        if(response.isSuccess()){
            logger.info("用户信息获取成功");
            return response;
        }
        return null;
    }

    /**
     * 认证授权服务端获取access_token、user_id
     */
    public static  AlipaySystemOauthTokenResponse getAccessToken(String authcode, Map<String,String> param) throws Exception{
        AlipayClient alipayClient = new DefaultAlipayClient(param.get("url"),param.get("appId"),
                param.get("appPrivateKey"),param.get("format"),param.get("charset"),
                param.get("alipayPublicKey"),param.get("signType"));
        logger.info("认证授权服务端获取 url:{},appId:{},appPrivateKey:{},format:{},charset:{},alipayPublicKey:{},signType:{}",
                param.get("url"),param.get("appId"), param.get("appPrivateKey"),param.get("format"),param.get("charset"),
                param.get("alipayPublicKey"),param.get("signType"));
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType(CustomerAlipayConstants.AUTHORIZATION_CODE);
        request.setCode(authcode);
        //request.setRefreshToken("201208134b203fe6c115 48bcabd8da5bb087a83b");
        AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
        return  response;
    }
    /**
     * 刷新授权服务端获取access_token、user_id
     */
    public static  AlipaySystemOauthTokenResponse getRefressToken(String refreshToken) throws Exception{
        AlipayClient alipayClient = new DefaultAlipayClient(CustomerAlipayConstants.URL,CustomerAlipayConstants.APP_ID,
                CustomerAlipayConstants.APP_PRIVATE_KEY,CustomerAlipayConstants.FORMAT,CustomerAlipayConstants.CHARSET,
                CustomerAlipayConstants.ALIPAY_PUBLIC_KEY,CustomerAlipayConstants.SIGN_TYPE);

        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType(CustomerAlipayConstants.REFRESH_TOKEN);
        request.setCode(refreshToken);
        AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
        return  response;
    }
}
